<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app"></div>
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <div id="app"></div>
    <script type="text/babel">
        // class CountDown extends React.Component {
        //     constructor(props){
        //         super(props)
        //         this.state = {
        //             time : 60,
        //             msg: '发送验证码',
        //             isTrue: false
        //         }
        //     }
        //     start = () =>{
        //         let {time,isTrue,msg} = this.state;
        //         isTrue = true;
        //         if(msg == '重新发送'){
        //             time = 59
        //         }
        //         let ms = setInterval(()=>{
        //             this.setState({
        //                 time : --time
        //             })
        //             if(time == 0 && isTrue){
        //                 this.setState({
        //                     msg : '重新发送',
        //                     time : 0,
        //                     isTrue : false
        //                 })
        //                 clearInterval(ms)
        //                 return
        //             }
        //         },1000)
        //     }
        //     render(){
        //         return (
        //             <button onClick={this.start}>{this.state.msg}:{this.state.time}</button>
        //         )
        //     }
        // }
        const useCountDown = (seconds) => {
            const {useState, useEffect, useRef} = React;
            const [time, setTime] = useState(seconds);
            const [disabled, setDisabled] = useState(false);
            const [msg, setMsg] = useState('发送验证码');
            const timer = useRef();
            const start = () => {
                setDisabled(true);
                clearInterval(timer.current);
                timer.current = setInterval(() => setTime((t) => --t), 1000)
            }
            useEffect(() => {
                if(disabled) {
                    if(time > 0 && time < seconds) {
                        setMsg(`${time}s后重发`);
                    } else {
                        setDisabled(false);
                        clearInterval(timer.current);
                        setTime(seconds);
                        setMsg('发送验证码')
                    }
                }
            }, [time]);
            return {
                start,
                msg,
                time,
                disabled
            }
        }
        const CountDownTimer = ({seconds}) => {
            const {disabled, start, msg} = useCountDown(seconds);
            return (
                <button disabled={disabled} onClick={start}>{msg}</button>
            )
        }
        ReactDOM.render(<CountDownTimer seconds={10} />, document.getElementById("app"))
    </script>
</body>

</html>